package com.ym521.sadcrash.core

import android.os.Handler
import android.os.Looper
import com.ym521.sadcrash.callback.CrashCallback

/**
 *@author Ym
 *E-mail: 2435970206@qq.com
 *createTime:2023/6/11
 *explain:
 * crash 的相关处理
 */
internal class CrashManager {
    var callBack: CrashCallback? = null

    fun register(am: ActivityManager) {
        Handler(Looper.getMainLooper()).post {
            while (true) {
                try {
                    Looper.loop()
                } catch (e: Throwable) {
                    am.currentActivity?.finish()
                    am.currentActivity = null
                    callBack?.onMainThread(e)
                }
            }
        }

        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            am.currentActivity?.finish()
            am.currentActivity = null
            callBack?.onChildMainThread(t,e)
        }

    }


}